/*
 * FindReplacePanel
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of FindReplacePanel.
 *
 * FindReplacePanel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FindReplacePanel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console.findreplace;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
class SearchQuery {

    static class Result {
        int from;
        int to;

        public Result(int from, int to) {
            this.from = from;
            this.to = to;
        }
    }
    private static final Result[] emptyRes = new Result[0];
    

    private SearchParams params;
    private String text = "";
    private int from;
    private int to;

    private Pattern pattern;
    private Matcher matcher;
    private Result[] results;


    public Result[] getAllResults() {
        if (results == null && matcher != null) {
            ArrayList<Result> list = new ArrayList<Result>();

            while (matcher.find()) {
                list.add(new Result(matcher.start() + from, matcher.end() + from));
            }
            
            results = (Result[]) list.toArray(emptyRes);
        }

        return results;
    }

    public Result findNext(int fromPosition) {
        if (results == null) getAllResults();
        if (results.length == 0) return null;

        for (int i = 0; i < results.length; i++) {
            Result next = results[i];
            if (next.from >= fromPosition) return next;
        }

        return results[0];
    }

    public Result findPrevious(int fromPosition) {
        if (results == null) getAllResults();
        if (results.length == 0) return null;

        for (int i = results.length - 1; i >= 0; i--) {
            Result prev = results[i];
            if (prev.from < fromPosition) return prev;
        }

        return results[results.length - 1];
    }

    /** Does not loop, unlike findNext().
     */
    public String replaceNext(int from, int to) {
        Matcher m = pattern.matcher(text.substring(from - this.from, to - this.from));
        return m.replaceFirst(params.replace);
    }

    public String replaceAll() {
        return matcher.replaceAll(params.replace);
    }
    
    private Pattern makePattern(SearchParams params) {
        String pattern = params.find;
        int flags = (params.matchCase ? 0 : Pattern.CASE_INSENSITIVE) | Pattern.UNICODE_CASE;

        if (params.regularExpression) {
            pattern = params.find;
            flags |= (params.regularExpression ? Pattern.MULTILINE : 0);
        }
        else if (params.wholeWords) {
            pattern = "\\b" + Pattern.quote(pattern) + "\\b";
        }
        else {
            flags |= Pattern.LITERAL;
        }

        return Pattern.compile(pattern, flags);
    }

    
    public int getFrom() {
        return from;
    }

    public int getTo() {
        return to;
    }

    public SearchParams getParams() {
        return params;
    }

    public void setParams(SearchParams p) {
        if (params != null && params.equals(p)) return;
        params = p;

        if (params != null && "".equals(params.find)) params = null;
        if (params != null) pattern = makePattern(params); else pattern = null;
        if (pattern != null) matcher = pattern.matcher(text); else matcher = null;

        results = null;
    }

    public String getText() {
        return text;
    }

    public void setText(String txt, int from, int to) {
        text = txt.substring(from, to);
        this.from = from;
        this.to = to;

        if (pattern != null) matcher = pattern.matcher(text); else matcher = null;

        results = null;
    }

    public boolean canSearch() {
        return (matcher != null);
    }

    public void disable() {
        setParams(null);
    }
}
